

#include "Global.h"
#include <TextureItem.h>
#include "ModelRCMgrIns.h"
#include "anim.h"
#include "Bone.h"
#include "SkelMeshNode.h"
#include "model.h"
#include "RigidMeshNode.h"
#include <GameEnvironment.h>
#include <ClientResourceManager.h>
#include "YB_DirectX.h"
//---------------------------------------------------------------------------------------------------------

CModelRCMgrIns::CModelRCMgrIns(void)
{
	Init();
}

CModelRCMgrIns::~CModelRCMgrIns(void)
{
	Term();
}

void CModelRCMgrIns::Init()
{
	m_pSkelMeshMgr  = new CModelRCMgr<CSkelMeshNode>(RC_MESH);
	m_pBoneMgr      = new CModelRCMgr<_Bone>(RC_BONE);
	m_pAnimMgr      = new CModelRCMgr<_Anim>(RC_ANIM);
	m_pNPCWeaponMgr = new CModelRCMgr<CRigidMeshNode>(RC_NPCWEAPON);
	m_pItemMgr      = new CModelRCMgr<CRigidMeshNode>(RC_TYPEMAX);
}
void CModelRCMgrIns::Term()
{
	delete m_pSkelMeshMgr;
	delete m_pBoneMgr;
	delete m_pAnimMgr;
	delete m_pNPCWeaponMgr;
	delete m_pItemMgr;
}

CSkelMeshNode* CModelRCMgrIns::GetSkelMesh(const _ModelRCDes* pDes)
{
	return m_pSkelMeshMgr->Get(pDes);
}

_Bone* CModelRCMgrIns::GetBone(const _ModelRCDes* pDes)
{
	return m_pBoneMgr->Get(pDes);
}

_Anim* CModelRCMgrIns::GetAnim(const _ModelRCDes* pDes)
{
	return m_pAnimMgr->Get(pDes);
}

CRigidMeshNode* CModelRCMgrIns::GetNPCWeapon(const _ModelRCDes* pDes)
{
	return m_pNPCWeaponMgr->Get(pDes);
}
CRigidMeshNode* CModelRCMgrIns::GetItem(const _ModelRCDes* pDes)
{
	return m_pItemMgr->Get(pDes);
}
CSkelMeshNode* CModelRCMgrIns::GetSkelMesh(const char* pszName)
{
	return m_pSkelMeshMgr->Get(pszName);
}

_Bone* CModelRCMgrIns::GetBone(const char* pszName)
{
	return m_pBoneMgr->Get(pszName);
}

_Anim* CModelRCMgrIns::GetAnim(const char* pszName)
{
	return m_pAnimMgr->Get(pszName);
}

CRigidMeshNode* CModelRCMgrIns::GetNPCWeapon(const char* pszName)
{
	return m_pNPCWeaponMgr->Get(pszName);
}

TextureItem* CModelRCMgrIns::GetTexture(const char* pszName,const int& iCharType)
{
	_ModelRCType eType = (iCharType == _NPC) ? RC_TEX_NPC : RC_TEXTURE;
	const char* pPath = CModelRCName::GetSingleton().GetFullPath(pszName,eType);

	D3DXIMAGE_INFO			imageinfo;
	if( FAILED( D3DXGetImageInfoFromFile( pPath, &imageinfo ) ) )
	{
		return NULL;
	}

	TextureItem* pTexture = ResourcePoolManager->CharacterTexturePool().findResource(pPath);
	if (pTexture && pTexture->getTexture())
	{
		return pTexture;
	}

	pTexture = ResourcePoolManager->CharacterTexturePool().createResource(pPath);

	uint32 uWidth = imageinfo.Width;
	uint32 uHeight = imageinfo.Height;
	D3DFORMAT Format = D3DFMT_A8R8G8B8;
	uint32 Filter    = D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR;
	uint32 mipFilter = D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR;

	//if (TOTALVIDMEM < 28)
	//{
	//	uWidth /= 2;
	//	uHeight /= 2;
	//}	

	if (GameEnvironment::GetRendererOption().m_texDXTCaps)
	{
		Filter    = D3DX_FILTER_NONE;
		mipFilter = D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR;
		Format = imageinfo.Format;		
	}
	//else
	//{
	//	Format = pDes->iPartType == _HAIR ? D3DFMT_A8R8G8B8 : D3DFMT_A1R5G5B5;	
	//}

	pTexture->setTextureParam(uWidth, uHeight, imageinfo.MipLevels, 0, Format, 0,Filter,mipFilter,D3DPOOL_MANAGED);
	pTexture->loadResource(pPath);

	return pTexture;
}


TextureItem* CModelRCMgrIns::GetTexture(const _ModelRCDes* pDes)
{
	_ModelRCType eType = (pDes->iCharType == _NPC) ? RC_TEX_NPC : RC_TEXTURE;
	const char* pszName = CModelRCName::GetSingleton().GetModelRCName(pDes,eType);
    return GetTexture(pszName,pDes->iCharType);
}

